﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace Upgrade
{
    public partial class Form1 : Form
    {
        private readonly System.Timers.Timer upgradeTimer = new System.Timers.Timer();
        private int progress = 0;
        private bool done = false;
        public Form1()
        {
            InitializeComponent();

            //this.textBox1.GotFocus += TextBox1_GotFocus;

            upgradeTimer.Interval = 300;
            upgradeTimer.Enabled = true;
            upgradeTimer.Elapsed += new ElapsedEventHandler(UpgradeTimer);
            upgradeTimer.AutoReset = true;
            upgradeTimer.Start();
        }

        private void UpgradeTimer(object sender, ElapsedEventArgs args)
        {
          /*   this.Invoke((EventHandler)delegate
            {
               this.progressBar1.Value = progress;
                this.label2.Text = progress + "%";
                if (done)
                {
                    this.label1.Text = "下载完成";
                    upgradeTimer.Stop();
                }
            });*/
        }
        public void DownloadFile(string URL, string filename)
        {
            Thread thread = new Thread((ThreadStart)delegate
            {
                try
                {
                    System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);
                    System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                    long totalBytes = myrp.ContentLength;
                    //if (prog != null)
                    //{
                    //   prog.Maximum = (int)totalBytes;
                    // }
                    System.IO.Stream st = myrp.GetResponseStream();
                    System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                    long totalDownloadedByte = 0;
                    byte[] by = new byte[1024];
                    int osize = st.Read(by, 0, (int)by.Length);
                    while (osize > 0)
                    {
                        totalDownloadedByte = osize + totalDownloadedByte;
                        // Application.DoEvents();
                        so.Write(by, 0, osize);
                        //if (prog != null)
                        //{
                        //    prog.Value = (int)totalDownloadedByte;
                        //}
                        osize = st.Read(by, 0, (int)by.Length);

                        progress = (int)(totalDownloadedByte / totalBytes * 100);
                        //label1.Text = "当前补丁下载进度" + percent.ToString() + "%";
                        //Application.DoEvents(); //必须加注这句代码，否则label1将因为循环执行太快而来不及显示信息
                    }
                    so.Close();
                    st.Close();
                    done = true;
                }
                catch (System.Exception)
                {
                    throw;
                }
            });
            thread.IsBackground = true;
            thread.Start();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DownloadFile("https://update.todesk.com/ToDesk_Setup.exe", "ToDesk.exe");
        }
        [DllImport("user32", EntryPoint = "HideCaret")]
        private static extern bool HideCaret(IntPtr hWnd);
        private void TextBox1_GotFocus(object sender, EventArgs e)
        {
           // HideCaret(this.textBox1.Handle);
        }
        private void TextBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
          //  HideCaret(this.textBox1.Handle);
        }

        private void TextBox1_MouseClick(object sender, MouseEventArgs e)
        {
           // HideCaret(this.textBox1.Handle);
        }

        private void TextBox1_MouseDown(object sender, MouseEventArgs e)
        {
           // HideCaret(this.textBox1.Handle);
        }
    }
}
